package v1

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

	bc "application/blockchain"
	"application/model"
	"application/pkg/app"
)

type AddHouseBody struct {
	Username   string  `json:"username" form:"username"`   //业主姓名
	AccountId  string  `json:"accountId" form:"accountId"` //业主id
	Area       float64 `json:"area" form:"area"`
	RealArea   float64 `json:"real_area" form:"real_area"`
	Address    string  `json:"address" form:"address"`
	AsAga      string  `json:"as_aga" form:"as_aga"`
	HouseType  string  `json:"house_type" form:"house_type"`
	Floor      int     `json:"floor" form:"floor"`
	HousePrice float64 `json:"house_price" form:"house_price"`
	IsFitment  int     `json:"is_fitment" form:"is_fitment"`
}

type DelHouseBody struct {
	HouseId int `json:"hid" form:"house_id"`
}

type HouseListBody struct {
	UserId int `json:"uid" form:"uid"`
	Page   int `json:"page" form:"page"`
	Size   int `json:"size" form:"size"`
}

type List struct {
	Houses []model.House `json:"list"`
	Page   int           `json:"page"`
}

// 添加房源信息
func AddHouse(c *gin.Context) {

	appG := app.Gin{C: c}
	body := new(AddHouseBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}

	var searchCondition [][]byte
	searchCondition = append(searchCondition, []byte(body.Username))
	searchCondition = append(searchCondition, []byte(body.AccountId))
	resp, err := bc.ChannelQuery("query-bad-record", searchCondition)
	if err != nil {
		appG.Response(http.StatusInternalServerError, "失败", err.Error())
		return
	}
	// 反序列化json
	var data []map[string]interface{}
	if err = json.Unmarshal(bytes.NewBuffer(resp.Payload).Bytes(), &data); err != nil {
		appG.Response(http.StatusInternalServerError, "失败", err.Error())
		return
	}

	if len(data) != 0 {
		appG.Response(http.StatusBadRequest, "该用户存在不良记录", "该用户存在不良记录")
		return
	}

	//追加参数
	var bodyBytes [][]byte
	bodyBytes = append(bodyBytes, []byte("5feceb66ffc8"))
	bodyBytes = append(bodyBytes, []byte(body.AccountId))
	bodyBytes = append(bodyBytes, []byte(strconv.FormatFloat(body.Area, 'E', -1, 64)))
	bodyBytes = append(bodyBytes, []byte(strconv.FormatFloat(body.RealArea, 'E', -1, 64)))
	//调用智能合约
	_, err = bc.ChannelExecute("createRealEstate", bodyBytes)
	if err != nil {
		appG.Response(http.StatusInternalServerError, "合约调用失败", err.Error())
		return
	}
	fmt.Println("123", Upload)
	house := model.House{
		UserName:    body.Username,
		UserId:      body.AccountId,
		Area:        body.Area,
		RealArea:    body.RealArea,
		HouseImages: Upload,
		Address:     body.Address,
		AsAga:       body.AsAga,
		HouseType:   body.HouseType,
		Floor:       body.Floor,
		HousePrice:  float32(body.HousePrice),
		IsFitment:   body.IsFitment,
	}
	res := model.AddHouse(house)
	if res == 0 {
		appG.Response(http.StatusOK, "房屋添加数据库失败", "房屋添加数据库失败")
		return
	}
	houseInfo, err := json.Marshal(house)
	if err != nil {
		appG.Response(http.StatusOK, "序列化失败", "序列化失败")
		return
	}
	//添加信息到延迟队列
	err = model.Produce(string(houseInfo), "house", "house")
	if err != nil {
		fmt.Println("房屋队列存放失败", err.Error())
		return
	}
	appG.Response(http.StatusOK, "添加房屋信息成功", res)
	return
}
